/**
 * Looker  新闻整合系统
 * Author  :solosky <solosky772@qq.com>
 * File    :Data.js
 * Date    :August 7, 2009
 * License : Apache License 2.0 
 */

/**
 *  工具包
 *  定义了一些常用的方法
 */
 
 var Util = {

	//字符串处理
	Str: {
		//填充指定的字符至指定的长度
		pad:function(str, pad, len, direction){
			if(!str || !pad || !len)
				return;
			if(!direction || (direction!='left' && direction!='right'))
				direction = "right";
			//如果字符串大于给定的填充长度，截取到指定的长度
			if(str.length>len){
				return direction=='left' ? str.substr(0,len) : str.substr(str.length-len-1,len);
			}
			var padding = new Array();
			var padLen  = len - str.length;
			for(i=0; i<padLen; i++)
				padding.push(pad);
			return direction=='left' ? padding.join("")+str : str+padding.join("");
		},
		Builder : function(){
			this.buf = new Array();
			this.append = function(str){
				this.buf.push(str);	
			};
			this.join = function(pad){
				return this.buf.join(pad);	
			};
			this.toString = function(){
				return 	this.join("");
			};
		},
		substr : function(str,start,len){
			return str.length >len ? str.substr(start,len)+".." : str;
		}
		
	},
	Arr : {
		cols:function(arr,col){
			var b  = new Array();
			if(arr instanceof Array){
				for(var i=0; i<arr.length; i++){
					if(typeof arr[i][col] !="undefined"){
						b.push(arr[i][col]);	
					}
				}
			}
			return b;
		},
		del : function(arr,obj,comp){
			//首先要找到这个元素
			for(var i=0; i<arr.length; i++){
				if(comp(obj,arr[i]))
					break;
			}
			if(i<arr.length){
				//找到后，逐个前移每一个元素
				for(;i<arr.length;i++){
					arr[i] = arr[i+1];	
				}
				arr.length--;
			}
		}
	},
	Obj:{
		clone : function(obj){
			switch(obj.constructor){
				case Array : return this.cloneArray(obj);break;
				case Object: return this.cloneObject(obj);break;
				case Function:
				case String:
				case Number:return obj; break;
				default: return obj; break;
			}
		},
		cloneArray : function(arr){
			var na = new Array();
			for(var i=0; i<arr.length; i++)
				na.push(this.clone(arr[i]));
			return na;
		},
		cloneObject : function(obj){
			var no = new Object();
			for(var v in obj){
				no[v] = this.clone(obj[v]);	
			}
			return no;
		}
		
	},

	
	Json : {
		//编码对象到字符串
		encode : function (obj){
			var tb = new Util.Str.Builder();
			for(var v in obj){
				if(obj[v]==null){
					tb.append(v+":null");
					continue;
				}
				var r = /function (.*?)\(\) .*/.exec(obj[v].constructor);
				switch(r[1].toLowerCase()){
					case "undefined":
						tb.append(v+":null");
						break;
					case "object" : 
						tb.append(v+":"+Util.Json.encode(obj[v]));
						break;
					case "string" : 
						tb.append(v+':"'+obj[v]+'"');
						break;
					case "array" :
						var ttb = new Util.Str.Builder();
						for(var i=0; i<obj[v].length; i++){
							var tt = (typeof obj[v][i]).toLowerCase();
							if(tt=="string")
								ttb.append('"'+obj[v][i]+'"');
							else if(tt=="number")
								ttb.append(obj[v][i]);
							else
								ttb.append(Util.Json.encode(obj[v][i]));
						}
						tb.append(v+":["+ttb.join(",")+"]");
						break;
					case "number":
						tb.append(v+":"+obj[v]);
						break;
					default:
						tb.append(v+":"+obj[v]);
						break;
				}
			}
			return "{"+tb.join(",")+"}";
			},
		//解码字符串到对象
		decode : function(str){
			var code = "var obj = "+str;
			eval(code);
			return obj;
		}
	},
	////end Json
	 
	 Cookie : {
		 //这两个函数都是jQuery Cookie插件中的，作者是 Klaus Hartl，表示感谢哈~ 我借来用用，并做了部分修改
		 //获取cookie
		 get:function (name){
				var result = null;
				if(document.cookie && $.trim(document.cookie) != ''){
				  var cookies = document.cookie.split(';');
				  var cookiesLen = cookies.length;
				  if(cookiesLen > 0){
					for(var i = 0; i < cookiesLen; i++){
					  var cookie = $.trim(cookies[i]);
					  if (cookie.substring(0, name.length + 1) == (name + '=')){
						result = decodeURIComponent(cookie.substring(name.length + 1));
						break;
					  }
					}
				  }
				}
				return result;
			  },
		//设置cookie
   		set : function(name, value){
					var s = Config.Cookie;
					var expires = '';
					var nType = 'number';
					if(s.expires && (typeof s.expires
					 == nType) || s.expires.toUTCString){
					   var date = null;
					   if(typeof s.expires == nType){
						 date = new Date();
						 date.setTime(date.getTime() + (s.expires*24*60*60*1000));
					   }else{
						 date = s.expires;
					   }
					   // use expires attribute, max-age is not supported by IE
					   expires = '; expires=' + date.toUTCString();
					}
					var path = s.path ? '; path=' + s.path : '';
					var domain = s.domain ? '; domain=' + s.domain : '';
					var secure = s.secure ? '; secure' : '';
					document.cookie = [name, '=', encodeURIComponent(value),
					 expires, path, domain, secure].join('');
					return true;
			}
	 },
	 /////end cookie
	 //加载器，用于 动态加载资源
	 Loader :{
		//动态加载css
		css : function(path){
			var link = '<link rel="stylesheet" type="text/css" href="'+path+'" media="screen" />';
			$("head").append(link);
		},
		js : function(path){
			var link = '<script type="text/javascript" src="'+path+'"></script>';
			$("head").append(link);
		} 
	 },
	 
	 //URL工具
	 URL :{
		 //根据给定的相对url，获取绝对url
		real : function(source,relative){
			return relative+source;///todo...
		}
	 },
	 
	 //Form工具
	 Form:{
		values : function(form){
			var es = form.elements;
			var vs = {};
			for(var i=0; i<es.length;i++){
				vs[es[i].name] = es[i].value;
			}
			return vs;
			
		}
	 }
	 
	 
 };
  